﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Zinnia.Common.Untilty
{
    /// <summary>
    /// 类型帮助类
    /// </summary>
    public sealed class TypeHelper
    {
        /// <summary>
        /// 获取当前应用程序域指定的类型集合
        /// </summary>
        /// <param name="interfaceType">接口类型</param>
        /// <returns></returns>
        public static IEnumerable<Type> GetCurrentDomainTypes(Type interfaceType)
        {
            return AppDomain.CurrentDomain.GetAssemblies()
            .SelectMany(a => a.GetTypes()
                .Where(t => t.GetInterfaces().Contains(interfaceType)));
        }

        /// <summary>
        /// 执行无参构造函数类型实例的方法
        /// </summary>
        /// <param name="type"></param>
        /// <param name="methodName"></param>
        /// <param name="paramameters"></param>
        /// <returns></returns>
        public static object InvokeNoParameterConstructMethod(Type type, string methodName, object[] paramameters)
        {
            var instance = Activator.CreateInstance(type);
            return type.GetMethod(methodName).Invoke(instance, paramameters);
        }
    }
}
